package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.commons.utils.TimeAgoUtils;
import com.qf.blog.commons.utils.UserThreadLocal;
import com.qf.blog.dto.CommentDto;
import com.qf.blog.dto.IndexDiscussPostDto;
import com.qf.blog.dto.MyPage;
import com.qf.blog.dto.TokenDto;
import com.qf.blog.entity.Comment;
import com.qf.blog.entity.DiscussPost;
import com.qf.blog.entity.User;
import com.qf.blog.mapper.DiscussPostMapper;
import com.qf.blog.mapper.UserMapper;
import com.qf.blog.service.DiscussPostService;
import com.qf.blog.vo.DiscussPostVo;
import com.qf.blog.vo.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;
import java.util.List;

/**
 * User: key
 * ClassName:DiscussPostServiceImpl
 * Date: 2023/6/3
 * Time: 17:12
 * description:
 */
@Service
public class DiscussPostServiceImpl extends ServiceImpl<DiscussPostMapper, DiscussPost> implements DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LikeService likeService;


    @Override
    public MyPage<IndexDiscussPostDto> indexDiscussPostData(IPage<IndexDiscussPostDto> page, Integer orderMode) {
        MyPage<IndexDiscussPostDto> indexPostDtoMyPage = baseMapper.indexPostDataPageVo(page, orderMode);
        List<IndexDiscussPostDto> records = indexPostDtoMyPage.getRecords();
        for (IndexDiscussPostDto record : records) {
            DiscussPost post = record.getPost();
            post.setCreateTimeStr(TimeAgoUtils.format(post.getCreateTime()));
            post.setLikeCount(likeService.getCount(post.getId()));

            // 换成字符串的时间
            post.setCreateTimeStr(TimeAgoUtils.format(post.getCreateTime()));
        }
        return indexPostDtoMyPage;
    }

    @Override
    public MyPage<IndexDiscussPostDto> indexDiscussPostData(IPage<IndexDiscussPostDto> page, Integer orderMode, String keyword) {
        MyPage<IndexDiscussPostDto> indexDiscussPostDtoMyPage = baseMapper.indexPostDataPageVoSearch(page, orderMode, keyword);
        List<IndexDiscussPostDto> records = indexDiscussPostDtoMyPage.getRecords();
        for (IndexDiscussPostDto record : records) {
            DiscussPost post = record.getPost();
            post.setCreateTimeStr(TimeAgoUtils.format(post.getCreateTime()));
        }
        return indexDiscussPostDtoMyPage;
    }

    @Override
    public MyPage<IndexDiscussPostDto> indexDiscussPostData(IPage<IndexDiscussPostDto> page, Integer orderMode, Integer entityId) {
        MyPage<IndexDiscussPostDto> indexDiscussPostDtoMyPage = baseMapper.indexPostDataPageVoPerson(page, orderMode, entityId);
        List<IndexDiscussPostDto> records = indexDiscussPostDtoMyPage.getRecords();
        for (IndexDiscussPostDto record : records) {
            DiscussPost post = record.getPost();
            post.setCreateTimeStr(TimeAgoUtils.format(post.getCreateTime()));
            post.setLikeCount(likeService.getCount(post.getId()));
        }
        return indexDiscussPostDtoMyPage;
    }


    @Override
    public List<CommentDto> getCommentListByPostId(Integer id) {
        List<CommentDto> commentDtos = baseMapper.getCommentListByPostId(id);
        for (CommentDto commentDto : commentDtos) {
            Comment comment = commentDto.getComment();

            comment.setCreateTimeStr(TimeAgoUtils.format(comment.getCreateTime()));

            // 查询每个评论的点赞状态和状态数量
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            User loginUser = (User) requestAttributes.getRequest().getSession().getAttribute("loginUser");
            boolean isLike = false;
            commentDto.setLikeStatus(isLike ? 1L : 0L);
        }
        return commentDtos;
    }

    @Override
    public List<IndexDiscussPostDto> adminPostList() {
        return baseMapper.adminPostList();
    }

    @Override
    public Integer updatePostType(Integer id, Integer type) {
        return baseMapper.updatePostType(id,type);
    }
    @Override
    public Integer publishDiscuss(DiscussPostVo discussPostVo) {
        Integer userId = discussPostVo.getUserId();
        discussPostVo.setCreateTime(new Date());
        discussPostVo.setStatus(0);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", userId));
        if (user == null){
            return 0;
        }
        return baseMapper.publishDiscuss(discussPostVo);
    }
    @Override
    public MyPage discussPage(MyPage page) {
        return discussPostMapper.discussPage(page);
    }

    @Override
    public Topic detail(Integer id) {
        Topic detail = discussPostMapper.detail(id);
        return detail;
    }

    @Override
    public Integer updatePostStatus(Integer id, Integer status) {
        return baseMapper.updatePostStatus(id,status);
    }

    @Override
    public void updateScore(Integer postId, Double score) {
        UpdateWrapper<DiscussPostVo> updateWrapper = new UpdateWrapper<DiscussPostVo>()
                .eq("id", postId)
                .set("score", score);
    }

}
